<?php

// 入口文件所在目录
// monitor.sh 基于此脚本进行 server 存活检测，请不要修改此脚本

define('SWROOT_PATH', dirname(__FILE__));
define('FRAMEWORK_PATH', dirname(dirname(dirname(__FILE__))));

$iniPath = FRAMEWORK_PATH . '/conf/serv_conf.ini';
$config = parse_ini_file($iniPath, true);

$_serverPort = $config['LISTENING']['port'];


$host = '127.0.0.1';  //where is the websocket server
$port = $_serverPort; 
//$data = '{"id": 2,"command": "server_info"}';  //data to be send

$head = "GET / HTTP/1.1"."\r\n".
    "Host: $host"."\r\n".
    "Upgrade: websocket"."\r\n".
    "Connection: Upgrade"."\r\n".
    "Sec-WebSocket-Key: asdasdaas76da7sd6asd6as7d"."\r\n".
    "Sec-WebSocket-Version: 13"."\r\n".
    "Content-Length: ".strlen($data)."\r\n"."\r\n";

////WebSocket handshake
$sock = fsockopen($host, $port, $errno, $errstr, 2);
fwrite($sock, $head ) or die('error:'.$errno.':'.$errstr);
$headers = fread($sock, 2000);
//fwrite($sock, "\x00$data\xff" ) or die('error:'.$errno.':'.$errstr);
//$wsdata = fread($sock, 2000);  //receives the data included in the websocket package "\x00DATA\xff"
//$retdata = trim($wsdata,"\x00\xff"); //extracts data
////WebSocket handshake
fclose($sock);

if (strpos($headers, 'swoole-websocket-server') !== false) {
    echo 'ok';
} else {
    echo $headers;
}

// monitor.sh 基于此脚本进行 server 存活检测，请不要修改此脚本
